import java.io.*;
import java.util.*;

public class Analyzer {

   /**
     * A helper method used internally to read user input from the standard input
     * @return a string containing the user's input
     */
    public static String getInput() {
        String s = "";
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        try {
            s = br.readLine();

        } catch (IOException e) {}
	if (s.toLowerCase().equals("quit"))
	    quit();
        return s.toLowerCase();
    }

 /**
     * Provides a way for the user to quit the program gracefully
     *
     */
    public static void quit() {
        System.out.println("\nTerminating program normally....\n\n");
        System.exit(0);
    }

   /**
     * Provides a menu-driven system for manipulating the
     * Simulator class. This method loops back to displaying
     * the menu after every simulation is run.
     *
     */
    public static void main(String[] args) {
	int numQueues;
	double arrivalProb;
	int itemLimit;
	int speed;
	int simMinutes;

	double result;

        while (true){
	    try {
	    System.out.println("\nWelcome to the Supermarket Simulator!\nType 'quit' at any time to exit.\n" );
	    System.out.print("Please enter the number of cashiers: ");
	    numQueues = Integer.parseInt(getInput());
	    System.out.print("\nPlease enter the probability that a customer will arrive each second: ");
	    arrivalProb = Double.parseDouble(getInput());
	    System.out.print("\nPlease enter the maximum number of items each customer may purchase: ");
	    itemLimit = Integer.parseInt(getInput());
	    System.out.print("\nPlease enter the number seconds it takes to scan each item: ");
	    speed = Integer.parseInt(getInput());
	    System.out.print("\nPlease enter the number of minutes to be simulated:  ");
	    simMinutes = Integer.parseInt(getInput());
	    }
	    catch(NumberFormatException e) {
		System.out.println("Invalid entry. Please try again.");
		continue;
	    }
	    System.out.println("Thank you. Simulating..."+ speed+ ", "+ itemLimit);
	    result = Simulator.simulate(numQueues, arrivalProb, itemLimit, speed, simMinutes);
	    System.out.println("\nSimulation complete. Average wait time was: " + result + " seconds.");

        }

    }
 

}
